package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.KJ.MyListView;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.OrderHistoryAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.OrderHistoryInfo;
import com.gzw.dms.bean.ShippingOrderInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QxScanActivity extends BaseActivity {
    private TextView tv_shiping_order_num, tv_custom_name, tv_custom_contact,
            tv_custom_tel, tv_receipt_name, tv_receipt_tel, tv_goods_name, tv_goods_num, tv_goods_weight,
            tv_goods_vol, tv_shipping_order_state, tv_is_recept, tv_remarks,tv_send_station
            ,tv_end_mechanism,tv_shiping_order_goid,tv_send_mechanism;
    private TextView tv_look;
    private EditText et_remarks;
    private Button cancel;
    private Intent mIntent;
    private ShippingOrderInfo shippingOrder;
    private Context mContext;
    private Toolbar toolbar;
    //0、1为接单，2已接单，3电子围栏签收，4实际签收,5分配订单
    private  String state;
    private  String fpstste="0";
    private List<String> ls_map;
    private LinearLayout layout_receipt;

    private String applicant_message;
    private LinearLayout layout_change;
    private LinearLayout layout_str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_qx_scan);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_shiping_order_num = (TextView) findViewById(R.id.ic_shiping_order_num).findViewById(R.id.tv_name);
        tv_shiping_order_goid = (TextView) findViewById(R.id.ic_shiping_order_goid).findViewById(R.id.tv_name);
        tv_send_station = (TextView) findViewById(R.id.ic_send_station).findViewById(R.id.tv_name);
//        tv_end_address = (TextView) findViewById(R.id.ic_end_address).findViewById(R.id.tv_name);
        tv_custom_name = (TextView) findViewById(R.id.ic_custom_name).findViewById(R.id.tv_name);
        tv_custom_contact = (TextView) findViewById(R.id.ic_custom_contact).findViewById(R.id.tv_name);
        tv_custom_tel = (TextView) findViewById(R.id.ic_custom_tel).findViewById(R.id.tv_name);
        tv_receipt_name = (TextView) findViewById(R.id.ic_receipt_name).findViewById(R.id.tv_name);
        tv_receipt_tel = (TextView) findViewById(R.id.ic_receipt_tel).findViewById(R.id.tv_name);
        tv_end_mechanism = (TextView) findViewById(R.id.ic_end_mechanism).findViewById(R.id.tv_name);
        tv_goods_name = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_left);
        tv_goods_num = (TextView) findViewById(R.id.ic_goods).findViewById(R.id.tv_right);
        tv_goods_weight = (TextView) findViewById(R.id.ic_goodsWV).findViewById(R.id.tv_left);
        tv_goods_vol = (TextView) findViewById(R.id.ic_goodsWV).findViewById(R.id.tv_right);
        tv_shipping_order_state = (TextView) findViewById(R.id.ic_shipping_order_state).findViewById(R.id.tv_left);
        tv_is_recept = (TextView) findViewById(R.id.ic_shipping_order_state).findViewById(R.id.tv_right);
        tv_send_mechanism= (TextView) findViewById(R.id.ic_send_mechanism).findViewById(R.id.tv_name);
        tv_remarks= (TextView) findViewById(R.id.ic_remarks).findViewById(R.id.tv_name);

        tv_look= (TextView) findViewById(R.id.tv_look);
        et_remarks = (EditText) findViewById(R.id.et_remarks);
        layout_receipt = (LinearLayout) findViewById(R.id.layout_receipt);
        cancel = (Button) findViewById(R.id.btn_cancel_scan);
        layout_change = ((LinearLayout) findViewById(R.id.layout_change));
        layout_str = ((LinearLayout) findViewById(R.id.layout_str));

    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("取消签收详情");
        mIntent = getIntent();
//        setSupportActionBar(toolbar);
        ls_map = new ArrayList<>();

        shippingOrder = (ShippingOrderInfo) mIntent.getExtras().get(
                "shippingOrder");
        if (shippingOrder.getWaybill_state()==1 || shippingOrder.getWaybill_state()==2){
            layout_change.setVisibility(View.GONE);
            layout_str.setVisibility(View.VISIBLE);
        }else {
            layout_str.setVisibility(View.GONE);

        }
        fpstste=(String)mIntent.getExtras().get("fpstste");
        setData(shippingOrder);

    }

    @Override
    protected void initEvents() {
        tv_look.setOnClickListener(new mOnClick());
        cancel.setOnClickListener(new mOnClick());
        toolbar.setNavigationOnClickListener(new mOnClick());
    }
    private void setData(ShippingOrderInfo shippingOrderInfo) {
        tv_shiping_order_num.setText("货运编号:" + "    " + shippingOrderInfo.getShiping_order_num());
        tv_shiping_order_goid.setText("出货订单号:" + "    " + shippingOrderInfo.getShiping_order_goid());
        tv_send_station.setText("起运位置:" + "    " + shippingOrderInfo.getSend_station());
//        tv_end_address.setText("终到位置:" + "    " + shippingOrderInfo.getEnd_address());
        tv_custom_name.setText("发货客户名称:" + "    " + shippingOrderInfo.getCustom_name());
        tv_custom_contact.setText("发货联系人:" + "    " + shippingOrderInfo.getCustom_contact());
        tv_custom_tel.setText("发货客户电话:" + "    " + shippingOrderInfo.getCustom_tel());
        tv_receipt_name.setText("收货客户名称:" + "    " + shippingOrderInfo.getReceipt_name());
        tv_receipt_tel.setText("收货客户电话:" + "    " + shippingOrderInfo.getReceipt_tel());
        tv_end_mechanism.setText("收货客户地址:" + "    " + shippingOrderInfo.getEnd_mechanism());
        tv_goods_name.setText("货物:" + "    " + shippingOrderInfo.getGoods_name());
        tv_goods_num.setText("总件数:" + "    " + shippingOrderInfo.getGoods_num());
        tv_goods_weight.setText("总重量:" + "    " + shippingOrderInfo.getGoods_weight() + "kg");
        tv_goods_vol.setText("总体积:" + "    " + shippingOrderInfo.getGoods_vol() + "m³");
        tv_send_mechanism.setText("受理机构:" + "    " + shippingOrderInfo.getSend_mechanism());
        //订单状态
        state = String.valueOf(shippingOrderInfo.getShipping_order_state());

        if(fpstste.equals("5")){
            tv_shipping_order_state.setText("运单状态:未出库");

        }else if(fpstste.equals("4")){
            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态:未出库");

            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");

            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");

            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");

            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");
            }else if (state.equals("5")) {
                tv_shipping_order_state.setText("运单状态:已返回");
            }else if (state.equals("6")) {
                tv_shipping_order_state.setText("运单状态:已收到");
            }
        } else if(fpstste.equals("6")){
            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态: 未出库");

            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");

            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");

            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");

            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");


            }else if (state.equals("5")) {
                tv_shipping_order_state.setText("运单状态:已返回");

            }else if (state.equals("6")) {
                tv_shipping_order_state.setText("运单状态:已收到");


            }
        }else if(fpstste.equals("7")){
            layout_receipt.setVisibility(View.GONE);

        } else {

            if (state.equals("0")) {
                tv_shipping_order_state.setText("运单状态:未出库");

            } else if (state.equals("1")) {
                tv_shipping_order_state.setText("运单状态:分配订单");

            } else if (state.equals("2")) {
                tv_shipping_order_state.setText("运单状态:司机接单");

            } else if (state.equals("3")) {
                tv_shipping_order_state.setText("运单状态:电子围栏签收");

            } else if (state.equals("4")) {
                tv_shipping_order_state.setText("运单状态:实际签收");

            }
        }
        tv_is_recept.setText("回单份数:" + "    " + shippingOrderInfo.getIs_recept());
        tv_remarks.setText("发货备注:" + "    " + shippingOrderInfo.getRemarks());

    }

    /**
     * Created by Wangzhiqiang on 2015/11/19.
     * button的点击方法
     */
    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.tv_look:
                    Intent mIntetn = new Intent();
                    Bundle bundle = new Bundle();
                    mIntetn.setClass(mContext, WebViewActivity.class);
                    bundle.putSerializable("shippingOrder", shippingOrder.getShiping_order_id());
                    bundle.putString("state", "1");
                    mIntetn.putExtras(bundle);
                    startActivityForResult(mIntetn, Integer.parseInt(state));

                    break;
                case R.id.btn_cancel_scan:

                    if (et_remarks.getText().toString().trim() == null
                            || et_remarks.getText().toString().trim().length() == 0) {

                        T.showShort(mContext, "取消原因不能为空！");
                        return;
                    }

                    applicant_message = et_remarks.getText().toString().trim();
                    ShowOrderTask showOrderTask = new ShowOrderTask();
                    showOrderTask.execute();
                    break;
                default:
                    finish();
                    break;
            }
        }
    }

    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("applicant_message",applicant_message );
            map.put("shiping_order_id", shippingOrder.getShiping_order_id());
            map.put("shiping_order_num",shippingOrder.getShiping_order_num());
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appAddCancelSignInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {

            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else if (result.indexOf("successMsg") > 0){
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    T.showShort(mContext, successMsg);
                    Intent mIntent = new Intent();
                    //把返回数据存入Intent
                    mIntent.putExtra("result", 1);
                    //设置返回数据
                    setResult(1, mIntent);
                    finish();
                }
            }
        }
    }



}
